// pages/visitPlanEdit/visitPlanEdit.js
const app = getApp()
const api = app.globalData.api
Page({

  /**
   * 页面的初始数据
   */
  data: {
    readOnly:false,
    isBack:false,
    clientId:'',
    clientName:'',
    currentTimestamp:'',
    currentDatestamp:'',
    time_popup_show:false,
    popup_show:false,
    date_popup_show:false,
    selectOption:[],
    remindTime:'无',
    startTime:'',
    endTime:'',
    nowTimeType:'',
    staffSelect:[],
    commit_name:'',
    commit_name_id:'',
    date:''
  },
  chooseStaff(){
    wx.navigateTo({
      url: '../staff/staff',
    })
  },
    /**删除协作人 */
    delStaff(e){
      let id = e.currentTarget.dataset.id;
      let staffSelect = this.data.staffSelect.filter(item=>item.id!==id);
      this.setData({
        staffSelect:staffSelect
      })
    },
  /**选项 */
  chooseWarn(e){
    let that = this;
    api.curlPost('?cla=clientAppClass&fun=params',{type:'remindTime'}).then((res)=>{
      that.setData({
        selectOption:res.data,
        popup_show:true,
      })
    })
  },

  /**选项取消 */
  onCancel(e){
    this.setData({
      popup_show:false,
    })
  },
  /**选项切换 */
  onConfirm(event){
    const { picker, value, index } = event.detail;
    this.setData({
      remindTime:value,
      popup_show:false
    })
  },

  /**选择时间 */
  chooseDate(e){
    let currentTimestamp  = this.data.date?new Date(this.data.date).getTime():new Date().getTime();
    this.setData({
      date_popup_show:true,
      currentDatestamp:currentTimestamp
    })
  },
  /**日期选择确认 */
  dateConfirm(e){
    this.setData({
      date:api.formatDateCRM('Y-m-d',e.detail),
      date_popup_show:false
    })
  },
  dateCancel(e){
    this.setData({
      date_popup_show:false
    })
  },
  

  /**选择时间 */
  chooseTime(e){
    let type = e.currentTarget.dataset.type;
    let currentTimestamp='';
    if(type=='startTime'){
        currentTimestamp = currentTimestamp = this.data.startTime?this.data.startTime:api.formatDateCRM('H:i');
    }else if(type=='endTime'){
        currentTimestamp = currentTimestamp = this.data.endTime?this.data.endTime:api.formatDateCRM('H:i');
    }
    this.setData({
      time_popup_show:true,
      currentTimestamp:currentTimestamp,
      nowTimeType:type
    })
  },
  /**日期选择确认 */
  timeConfirm(e){
    if(this.data.nowTimeType=='startTime'){
      this.setData({
        startTime:e.detail+':00',
        time_popup_show:false
      })
    }else if(this.data.nowTimeType=='endTime'){
      this.setData({
        endTime:e.detail+':00',
        time_popup_show:false
      })
    }
  },
  timeCancel(e){
    this.setData({
      time_popup_show:false
    })
  },
  
  /**选择客户 */
  chooseClient(){
    wx.navigateTo({
      url: '../chooseClient/chooseClient',
    })
  },

  /**提交 */
  commit(e){
    let para = e.detail.value;
    let staffId = [];
    this.data.staffSelect.forEach((v,i)=>{
      staffId.push(v.id)
    })
    para.member = staffId.join(',');
    para.khid=this.data.clientId;
    para.id=this.data.id?this.data.id:'';
    para.date=this.data.date;
    para.startTime=this.data.date+' '+para.startTime;
    para.endTime=this.data.date+' '+para.endTime;
    api.curlPost('?cla=clientAppClass&fun=visitPlanEdit',para).then((res)=>{
      wx.navigateBack({
        delta:1
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let id = options.id;
    let khid = options.khid;
    let companyName = options.clientName;
    let date = options.date;
    if (!api.isBlank(companyName)) {
      this.setData({
        // readOnly:true,
        clientName:companyName,
        clientId:khid,
        id:id,
      })
    }
    date = !api.isBlank(date)?date:api.formatDateCRM('Y-m-d');
    this.setData({
      date
    })
    let that = this;
    if(!api.isBlank(id)){
      api.curlPost('?cla=clientAppClass&fun=detailVisitPlan',{id:id}).then((res)=>{
        that.setData({
          info:res.data,
          id:res.data.id,
          date:res.data.locationDate,
          staffSelect:res.data.member,
          clientName:res.data.companyName,
          clientId:res.data.khid,
          startTime:api.formatDateCRM('H:i',res.data.startTime),
          endTime:api.formatDateCRM('H:i',res.data.endTime),
          remindTime:res.data.remindTime
        })
      })
    }
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    if(this.data.isBack && this.data.commit_name_id){
      let flag = false;
      let resultArr=[];
      this.data.staffSelect.forEach((item)=>{
        if(item.id == that.data.commit_name_id){
          flag = true;
        }
        resultArr.push(item);
      })
      if(!flag){
        resultArr.push({id:this.data.commit_name_id,name:this.data.commit_name})
      }
      this.setData({
        staffSelect:resultArr,
        commit_name:'',
        commit_name_id:''
      })
    }
    this.setData({
      isBack:false,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})